from django.shortcuts import render
from .ml import MachineLearn
from django.http import JsonResponse


# Create your views here.
def index(request):
    """鸢尾花首页请求"""
    return render(request, 'iris/index.html')


def linear_pred(request):
    """线性回归预测花瓣长度请求"""
    petal_width = float(request.POST.get("petal_width", None))
    linear_select = request.POST.get("linear_select", None)
    ml = MachineLearn()
    # print(f"花瓣宽度：{petal_width},线性回归选择:{linear_select}")
    res = ""
    if linear_select == "LinearRegression":
        """线性回归模型"""
        res = ml.linearRegression(petal_width)
    else:
        """多项式线性回归模型"""
        res = ml.polyRegresson(petal_width)

    return JsonResponse({"msg": res})


def pred(request):
    """分类预测的请求"""
    #取得前端传递过来的 花的参数
    petal_width2 = float(request.POST.get('petal_width2', None))
    petal_length = float(request.POST.get('petal_length', None))
    sepal_width = float(request.POST.get('sepal_width', None))
    sepal_length = float(request.POST.get('sepal_length', None))
    logic_select = request.POST.get('logic_select', None)

    ml = MachineLearn()
    res_pred=""
    if logic_select=="KNN":
        res_pred=ml.knn([[sepal_length,sepal_width,petal_length,petal_width2]])
    return JsonResponse({"msg":f"预判的类型:{res_pred[0]}","acc":f"精确度:{res_pred[1]}"})